﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PROAD.admin.Relatorio
{
    public partial class RelatorioAtmMedia : System.Web.UI.Page
    {
        DADOS.auxiliares.metRelatorio _metRel = new DADOS.auxiliares.metRelatorio();
        DADOS.auxiliares.metEndereco metAux = new DADOS.auxiliares.metEndereco();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CarregarDdl();
            }
        }

        private void CarregarDdl()
        {
            ddlUf.DataSource = metAux.ListaUF();
            ddlUf.DataBind();
            ddlUf.Items.Insert(0, new ListItem("Selecione uma UF", "0"));


        }

        protected void CarregarGrid() 
        {
            PnlConsulta.Visible = false;
            pnlResult.Visible = true;

            var objAtmEq = _metRel.ListarAtmEq(Convert.ToInt32(ddlUf.SelectedValue), txtDtInicial.Text,txtDtFinal.Text);

            gdrAtmEq.DataSource = objAtmEq;
            gdrAtmEq.DataBind();

            var objAtmEqAb = _metRel.ListarAtmEqAb(Convert.ToInt32(ddlUf.SelectedValue), txtDtInicial.Text, txtDtFinal.Text);

            grdAtmAb.DataSource = objAtmEqAb;
            grdAtmAb.DataBind();

            var objAtmMac = _metRel.ListarAtmEqMac(Convert.ToInt32(ddlUf.SelectedValue), txtDtInicial.Text, txtDtFinal.Text);

            grdAtmMac.DataSource = objAtmMac;
            grdAtmMac.DataBind();
        }

        protected void btnPesquisar_Click(object sender, EventArgs e)
        {
            CarregarGrid();
        }

        protected void gdrAtmEq_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if(e.Row.RowType == DataControlRowType.DataRow)
            {
                decimal result = 0;
                try
                {
                    result =Convert.ToDecimal(e.Row.Cells[0].Text) / Convert.ToDecimal(e.Row.Cells[1].Text);
                    e.Row.Cells[2].Text = result.ToString("0.##");
                }
                catch (Exception)
                {

                    e.Row.Cells[2].Text = result.ToString("0.##");
                }
            }
        }

        protected void grdAtmAb_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                decimal result = 0;
                try
                {
                    result = Convert.ToDecimal(e.Row.Cells[0].Text) / Convert.ToDecimal(e.Row.Cells[1].Text);
                    e.Row.Cells[2].Text = result.ToString("0.##");
                }
                catch (Exception)
                {

                    e.Row.Cells[2].Text = result.ToString("0.##");
                }
            }
        }

        protected void grdAtmMac_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                decimal result = 0;
                try
                {
                    result = Convert.ToDecimal(e.Row.Cells[0].Text) / Convert.ToDecimal(e.Row.Cells[1].Text);
                    e.Row.Cells[2].Text = result.ToString("0.##");
                }
                catch (Exception)
                {

                    e.Row.Cells[2].Text = result.ToString("0.##");
                }
            }
        }

        protected void btnNovaPesquisa_Click(object sender, EventArgs e)
        {
            PnlConsulta.Visible = true;
            pnlResult.Visible = false;
            ddlUf.SelectedValue = "0";
            txtDtFinal.Text = string.Empty;
            txtDtInicial.Text = string.Empty;

        }
    }
}